<?php

namespace Cps\Controller;

use Think\Controller;
use Think\Page;

class MemberController extends Controller {

    public function __construct() {
        parent::__construct();
        session("[start]");
        if (!isset($_SESSION['cpsinfo']) || $_SESSION['cpsinfo']['id'] < 1) {
            $this->redirect('Login/index');
        }
    }

    public function index() {

        $cpsinfo = $_SESSION['cpsinfo'];
        if ($cpsinfo['refer'] == 0) {
            $refer = I("request.refer");
            $model = M('cps_member');
            $refername = $model->field('username')->where("refer='%s'", $refer)->find();
            $this->assign('refname', $refername['username']);
            $this->assign('refer', $refer);
        } else {
            $refer = $cpsinfo['refer'];
        }
        //var_dump($cpsinfo);exit;
        $game_id = $_POST['game_id'] ? intval($_POST['game_id']) : 0;
        $area_num = $_POST['area_num'] ? intval($_POST['area_num']) : 0;
        $username = $_POST['username'] ? trim(I('post.username')) : '';
        $dt1 = $_POST['data_start'] ? I('post.data_start') : '';
        $dt2 = $_POST['data_end'] ? I('post.data_end') : '';
        //游戏信息
        $gameModel = M('game_list');
        $game = $gameModel->field('id,gamename')->order('id desc')->select();
        $gamelist = json_encode($game);
        $this->assign('game', $gamelist);

        //用户游戏关系表，游戏表，分区表
        $count = count($this->get_member($refer));


        $Page = new Page($count, 10); // 实例化分页类 传入总记录数
        $show = $Page->show(); // 分页显示输出
        $list = $this->get_member($refer, 1, $Page->firstRow, $Page->listRows, $game_id, $area_num, $username, strtotime($dt1), strtotime($dt2));
        $this->assign('game_id', $game_id);
        $this->assign("area_num", $area_num);
        $this->assign("username", $username);
        $this->assign('dt1', $dt1);
        $this->assign('dt2', $dt2);
        $this->assign('page', $show);
        $this->assign('total', count($list));
        $this->assign('list', $list);
        $this->display();
    }

    private function get_member($refer, $flag = 0, $firstRow, $listRows, $game_id, $area_num, $username, $starttime, $endtime) {

        $member = M('game_user_relation');
        if ($flag == 0) {
            $sql = "SELECT u.username,g.gamename,a.areaname,u.reg_time,u.reg_ip,gur.rolelevel FROM user_info AS u , game_user_relation AS gur , game_list AS g , game_server_list AS a WHERE u.register_type IN (" . $refer . ") AND u.uid = gur.uid AND gur.game_id = g.id AND gur.game_id = a.game_id AND gur.area_num = a.area_num";
            return $member->query($sql);
        } else {
            $sql = "SELECT u.username,g.gamename,a.areaname,u.reg_time,u.reg_ip,gur.rolelevel FROM user_info AS u , game_user_relation AS gur , game_list AS g , game_server_list AS a ";
            $where = " WHERE u.register_type IN (" . $refer . ") AND u.uid = gur.uid AND gur.game_id = g.id AND gur.game_id = a.game_id AND gur.area_num = a.area_num";

            if ($game_id != 0) {
                $where.=" and gur.game_id='" . $game_id . "'";
            }
            if ($area_num != 0) {
                $where.=" and gur.area_num='" . $area_num . "'";
            }
            if ($username != '') {
                $where.=" and gur.username='" . $username . "'";
            }
            if ($starttime != '') {
                $where.=" and u.reg_time>='" . $starttime . "'";
            }
            if ($endtime != '') {
                $where.=" and u.reg_time<='" . $endtime . "'";
            }
            $limit = " limit {$firstRow} , {$listRows}";
            $query = $sql . $where . $limit;
            //echo $query;exit;
            return $member->query($query);
        }
    }

}
